unit data_master;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, Tabs, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection, StdCtrls, Grids, Setting, Unit_External, user_tambah, user_ubah,
  frxClass, PPS_Tambah, PPS_Edit;

type
  TForm_Data_Master = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    TabSheet5: TTabSheet;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZQuery2: TZQuery;
    ZTable1: TZTable;
    ZTable2: TZTable;
    StringGrid1: TStringGrid;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    frxReport1: TfrxReport;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button77: TButton;
    Button88: TButton;
    kabupatencombo: TComboBox;
    kecamatancombo: TComboBox;
    desacombo: TComboBox;
    table: TStringGrid;
    ZConnection2: TZConnection;
    ZQuery3: TZQuery;
    ZTable3: TZTable;
    procedure FormCreate(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure kabupatencomboChange(Sender: TObject);
    procedure kecamatancomboChange(Sender: TObject);
    procedure desacomboChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure refresh;
    procedure tampilpetugas;
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button77Click(Sender: TObject);
    procedure Button88Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button6Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);



  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; kode_get:String);             overload;
  end;

var
  Form_Data_Master: TForm_Data_Master;
  sdesa, skec, skota, textdesa,textkec, textkota, kode_kabkot:string;
  listid,listiduser:tstringlist;


implementation

{$R *.dfm}


constructor TForm_Data_Master.Create(AOwner: TComponent; kode_get:String);
begin
    kode_kabkot:=kode_get;
    inherited Create(AOwner);
end;

procedure TForm_Data_Master.desacomboChange(Sender: TObject);
var
  desa:String;
  query:string;
  kode:string;
begin
  if (desacombo.ItemIndex > 0) then begin
    desa := desacombo.Items[desacombo.ItemIndex];
    textdesa:=desa;
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(desa)+' AND KODE_KECAMATAN = '+QuotedStr(skec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sDesa := ZTable1.FieldByName('KODE_DESA').AsString;
  end
    else
  begin
    sDesa:='';
    textdesa:='';
  end;
end;

procedure TForm_Data_Master.Button1Click(Sender: TObject);
begin
  refresh;

end;
procedure TForm_Data_Master.Button2Click(Sender: TObject);
var tambah:TPPS_Form;
begin
  tambah:=TPPS_Form.Create(application);

end;

procedure TForm_Data_Master.Button3Click(Sender: TObject);
var edit:TPPS_Edit_Form;
begin
  if((table.Row>0)and(table.Row<=listid.Count))then
  begin
    edit:=TPPS_Edit_Form.Create(application, table.Cells[2,table.Row], table.Cells[3,table.Row], table.Cells[4,table.Row], table.Cells[5,table.Row], table.Cells[1,table.Row], table.Cells[6,table.Row],listid[table.Row-1]);
  end;
end;

procedure TForm_Data_Master.refresh;
var
  i:Integer;
begin
    if (desacombo.ItemIndex > 0) then begin
    with ZTable1 do
    begin
      active:=false;
      tablename:='petugas';
      filter:='KODE_DESA='+QuotedStr( sdesa);
      filtered:=true;
      active:=true;
      tampilpetugas;
    end;
  end
  else if (kecamatancombo.ItemIndex > 0) then begin
    with ZTable1 do
    begin
      active:=false;
      tablename:='petugas';
      filter:='KODE_KECAMATAN='+QuotedStr(skec);
      filtered:=true;
      active:=true;
      tampilpetugas;
    end;
  end
  else if (kabupatencombo.ItemIndex > 0) then begin
    with ZTable1 do
    begin
      active:=false;
      tablename:='petugas';
      filter:='KODE_KABKOT='+QuotedStr(skota);
      filtered:=true;
      active:=true;
      tampilpetugas;
    end;
  end;
  for i := 0 to table.ColCount - 1 do
      AutoSizeCol(table, i);
end;
procedure TForm_Data_Master.StringGrid1Click(Sender: TObject);
var
  I: Integer;
begin
end;

procedure TForm_Data_Master.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;


procedure TForm_Data_Master.tampilpetugas();
var counter:integer;
begin
  counter:=1;
  with ztable1 do
  begin
    table.RowCount:=RecordCount+1;
    table.ColCount:=7;
    table.Cells[0,0]:='No';
    table.Cells[1,0]:='Nama';
    table.Cells[2,0]:='Posisi';
    table.Cells[3,0]:='Kota/Kabupaten';
    table.Cells[4,0]:='Kecamatan';
    table.Cells[5,0]:='Kelurahan/Desa';
    table.Cells[6,0]:='Jabatan';

    first;

    listid:=tstringlist.Create;
    listid.Clear;

    while (not eof) do
    begin
      listid.Add(FieldByName('ID_PETUGAS').AsString);
      table.Cells[0,counter]:=inttostr(counter);
      table.Cells[1,counter]:=FieldByName('NAMA').AsString;
      if(FieldByName('POSISI').AsString='1')then
        table.Cells[2,counter]:='KPU Prov'
      else if(FieldByName('POSISI').AsString='2')then
        table.Cells[2,counter]:='KPU Kab/Kot'
      else if(FieldByName('POSISI').AsString='3')then
        table.Cells[2,counter]:='PPK'
      else if(FieldByName('POSISI').AsString='4')then
        table.Cells[2,counter]:='PPS';
      table.Cells[3,counter]:=textkota;
      table.Cells[4,counter]:=textkec;
      table.Cells[5,counter]:=textdesa;
      if(FieldByName('JABATAN').AsString='1')then
        table.Cells[6,counter]:='Ketua'
      else if(FieldByName('JABATAN').AsString='2')then
        table.Cells[6,counter]:='Anggota';
      counter:=counter+1;
      next;
    end;
  end;
end;
procedure TForm_Data_Master.Button4Click(Sender: TObject);
var form : TForm;
begin
  form:= TForm_user_tambah.Create(application);
end;

procedure TForm_Data_Master.Button5Click(Sender: TObject);
var form : TForm;
begin
  showmessage(listiduser[StringGrid1.Row]);
  form:= TForm_user_ubah.Create(application);
end;


procedure TForm_Data_Master.Button6Click(Sender: TObject);
begin
  if((StringGrid1.Row>0)and(StringGrid1.Row<=listid.Count))then
  begin
    if(MessageDlg('Apakah Anda yakin akan menghapus user tersebut?',mtError, mbOKCancel, 0)=mrOk)then
    begin
      with ZQuery1 do
      begin
        active:=false;
        sql.Clear;
        sql.Text:='delete from petugas where ID_PETUGAS='+QuotedStr(listid[StringGrid1.Row-1]);
        ExecSQL;
      end;
      refresh;
    end;
  end;

end;

procedure TForm_Data_Master.Button77Click(Sender: TObject);
begin
  if((table.Row>0)and(table.Row<=listid.Count))then
  begin
    if(MessageDlg('Apakah Anda yakin akan menghapus petugas tersebut?',mtError, mbOKCancel, 0)=mrOk)then
    begin
      with ZQuery1 do
      begin
        active:=false;
        sql.Clear;
        sql.Text:='delete from petugas where ID_PETUGAS='+QuotedStr(listid[table.Row-1]);
        ExecSQL;
      end;
      refresh;
    end;
  end;

end;

procedure TForm_Data_Master.Button88Click(Sender: TObject);
begin
  Close;

end;

procedure TForm_Data_Master.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TForm_Data_Master.FormCreate(Sender: TObject);
var
  setting:TSetting_Form;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ztable2.Connection:=ZConnection1;
  ZQuery2.Connection:=ZConnection1;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;

  PageControl1.Pages[0].Caption:='Petugas Pemilu';
  PageControl1.Pages[1].Caption:='Desa/Kelurahan';
  PageControl1.Pages[2].Caption:='Kecamatan';
  PageControl1.Pages[3].Caption:='Kota/Kabupaten';
  PageControl1.Pages[4].Caption:='User';
  if(kode_kabkot<>'00')then
    PageControl1.Pages[3].TabVisible:=false;

end;

procedure TForm_Data_Master.kabupatencomboChange(Sender: TObject);
var
  pilihan:String;
begin
  if (kabupatencombo.ItemIndex > 0) then
  begin
    pilihan := kabupatencombo.Items[kabupatencombo.ItemIndex];
    textkota:=pilihan;
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        skota := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(skota);
        Filtered := True;
        Active := True;
        First;
      end;
    kecamatancombo.Clear;
    kecamatancombo.Items.Add('PILIH KECAMATAN');
    kecamatancombo.ItemIndex:=0;
    desacombo.Clear;
    desacombo.Items.Add('PILIH KELURAHAN/DESA');
    desacombo.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        kecamatancombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    skota:='';
    textkota:='';
    kecamatancombo.Clear;
    desacombo.Clear;
  end;
  textkec:='';
  textdesa:='';
  sKec:='';
  sDesa:='';
end;

procedure TForm_Data_Master.kecamatancomboChange(Sender: TObject);
var
  pilihan:String;
begin
  if (kecamatancombo.ItemIndex > 0) then
  begin
    pilihan := kecamatancombo.Items[kecamatancombo.ItemIndex];
    textkec:=pilihan;
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+ ' and KODE_KABKOT = ' + QuotedStr
      (skota);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    desacombo.Clear;
    desacombo.Items.Add('PILIH KELURAHAN/DESA');
    desacombo.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        desacombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKec:='';
    textkec:='';
    desacombo.Clear;
  end;
  textdesa:='';
  sDesa:='';
end;

procedure TForm_Data_Master.PageControl1Change(Sender: TObject);
var
  ext:ExternalFile;
  i,countergrid:integer;
begin
  if(PageControl1.TabIndex=0)then    //petugas pemilu
  begin

    skota:='';
    sKec:='';
    sDesa:='';
    textkota:='';
    textKec:='';
    textDesa:='';

    desacombo.items.Add('Pilih Kel/Desa');
    //jabatancombo.items.Add('Pilih Jabatan');
    //posisicombo.Items.Add('Pilih Posisi');
    kabupatencombo.Items.Add('Pilih Kab/Kota');
    Kecamatancombo.Items.Add('Pilih Kecamatan');
    desacombo.ItemIndex:=0;
    kecamatancombo.ItemIndex:=0;
    kabupatencombo.ItemIndex:=0;


    ZTable1.Active := False;
    ZTable1.TableName := 'kabkot';
    ZTable1.Active := True;
    ZTable1.First;
    while (not ZTable1.Eof) do
      begin
        kabupatencombo.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
  else if(PageControl1.TabIndex=1)then  // desa/kelurahan
  begin

  end
  else if(PageControl1.TabIndex=2)then   // kecamatan
  begin

  end
  else if((kode_kabkot='00')and(PageControl1.TabIndex=3))then   // kabupaten
  begin

  end
  else if(((kode_kabkot='00')and(PageControl1.TabIndex=4))or
  ((kode_kabkot<>'00')and(PageControl1.TabIndex=3))) then // user
  begin
    countergrid:=1;
    ZTable1.Active := False;
    ZTable1.TableName := 'user';
    ZTable1.Active := True;
    StringGrid1.Cells[0,0]:='No';
    StringGrid1.Cells[1,0]:='Username';
    StringGrid1.Cells[2,0]:='Password';
    StringGrid1.Cells[3,0]:='Kota/Kabupaten';
    StringGrid1.RowCount:=ZTable1.RecordCount+1;
    StringGrid1.ColCount:=4;
    listiduser:=tstringlist.Create;
    ZTable1.First;
    while (not ZTable1.Eof) do
      begin
        if(ZTable1.FieldByName('KODE_KABKOT').AsString='00')then
        begin
          StringGrid1.Cells[3,countergrid]:='Provinsi';
        end
        else
        begin
          with ztable2 do
          begin
            active:=false;
            tablename:='kabkot';
            filter:='KODE='+QuotedStr(ZTable1.FieldByName('KODE_KABKOT').AsString);
            filtered:=true;
            active:=true;
            first;
            StringGrid1.Cells[3,countergrid]:=FieldByName('TAMPIL').AsString;
          end;

        end;
        StringGrid1.Cells[0,countergrid]:=inttostr(countergrid);
        StringGrid1.Cells[1,countergrid]:=ZTable1.FieldByName('USERNAME').AsString;
        StringGrid1.Cells[2,countergrid]:='********';
        listiduser.Add(ZTable1.FieldByName('ID_USER').AsString);
        countergrid:=countergrid+1;
        ZTable1.Next;
      end;
  end;

end;

end.
